package cn.itcast.mybatis;


import cn.itcast.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * *
 * Created by IntelliJ IDEA.
 * Author: caolei
 * Date: 2018/9/8
 * Time: 13:55
 * *
 */
public class mybatisTest {
    public static void main(String[] args) throws IOException {
       /* String resource = "org/mybatis/example/mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession session = sqlSessionFactory.openSession();
       try {
       Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
      } finally {
       session.close();
      }
       */


       //1.获取配置文件的输入流
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //2.通过输入流获取sqlsessionfactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //3.通过sqlsessionfactory获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.通过sqlsession操作数据库
        User user = sqlSession.selectOne("UserMapper.queryUserById", 1L);
        System.out.println(user);
        sqlSession.close();


    }
}
